﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SMSPanasonic.Business
{
    public partial class MessagePattern
    {
        #region Singletion pattern
        private static Dictionary<string, string> _instance;
        private static readonly object _lock = new object();

        public static string GetMessage(string key)
        {
            if (_instance == null)
            {
                lock (_lock)
                {
                    _instance = new Dictionary<string, string>();
                    using (SqlDataReader rd = SqlHelper.ExecuteReader(AppConfig.ConnectionString, CommandType.Text,
                        "SELECT PatternCode, PatternMessage FROM dbo.pa_MessagePattern"))
                    {
                        while (rd.Read())
                        {
                            _instance.Add(rd["PatternCode"].ToString(), rd["PatternMessage"].ToString());
                        }
                    }
                }
            }
            if (_instance.ContainsKey(key))
                return _instance[key];
            else
            {
                lock (_lock)
                {
                    SQLCommand.ExecuteNonQuery("INSERT INTO dbo.pa_MessagePattern(PatternCode, PatternMessage) VALUES(@PatternCode, @PatternCode)", new SqlParameter("@PatternCode", key));
                    _instance.Add(key, key);
                }
                return _instance[key];
            }
        }

        public static void ReloadData()
        {
            using (SqlConnection conn = new SqlConnection(AppConfig.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT PatternCode, PatternMessage FROM dbo.pa_MessagePattern", conn))
                {
                    ReloadData(cmd);
                }
            }
        }

        public static void ReloadData(SqlCommand cmd)
        {
            lock (_lock)
            {
                if (_instance != null)
                    _instance.Clear();
                else
                    _instance = new Dictionary<string, string>();
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        _instance.Add(rd["PatternCode"].ToString(), rd["PatternMessage"].ToString());
                    }
                }
            }
        }
        #endregion Singleton pattern

        public static string GetMessage(pa_WarrantyWaiting objwaiting)
        {
            try
            {
                if (objwaiting.WarrantyType == eWarrantyType.DK)
                {
                    return GetMessageDK(objwaiting);
                }
                else if (objwaiting.WarrantyType == eWarrantyType.GH)
                {
                    return GetMessageGH(objwaiting);
                }
                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        private static string GetMessageDK(pa_WarrantyWaiting objwaiting)
        {
            string pattern = GetMessage(eMessagePattern.MAUDANGKY);
            var model = pa_Model.GetOne(objwaiting.ModelCode);
            if (model != null)
            {
                return pattern
                .Replace("(Model)", objwaiting.ModelCode)
                .Replace("(SoMay)", objwaiting.ProductCode)
                .Replace("(NgayDangKy)", objwaiting.RegisterDate.ToString("dd/MM/yyyy"))
                .Replace("(HanBaoHanh)", model.Description.ToVietnameseWithoutAccent());
            }
            return string.Empty;
        }

        private static string GetMessageGH(pa_WarrantyWaiting objwaiting)
        {
            string pattern = GetMessage(eMessagePattern.MAUGIAHAN);
            var package = pa_WarrantyPackage.GetOne(objwaiting.WarrantyPackageCode);
            if (package != null)
            {
                DateTime? extentRegisterDate = pa_Warranty.GetWarrantyExtentRegisterDate(objwaiting.ModelCode, objwaiting.ProductCode, objwaiting.RegisterDate);
                if (extentRegisterDate != null)
                {
                    return pattern
                            .Replace("(Model)", objwaiting.ModelCode)
                            .Replace("(SoMay)", objwaiting.ProductCode)
                            .Replace("(HanBaoHanh)", package.WarrantyPeriod.ToString())
                            .Replace("(NgayDangKy)", extentRegisterDate.Value.ToString("dd/MM/yyyy"));
                }
            }
            return string.Empty;
        }
    }
}
